package com.nanbei.dp;

import java.util.Arrays;

/**
 * @ClassDescription: 两个字符串的最长 公共子序列 的长度 （动态规划）
 * @JdkVersion: 17
 * @Author: BaoShan.Li
 * @Date: 2024/7/9 23:35
 */
public class LCSubsequence {

    /*
                      a  c  e
                   0  0  0  0
                a  0  1  1  1
                b  0  1  1  1
                c  0  1  2  2
                d  0  1  2  2
                e  0  1  2  3

    * */

    public static void main(String[] args) {
        System.out.println(longestCommonSubsequence("abcde", "ace"));
    }

    public static int longestCommonSubsequence(String text1, String text2) {
        int[][] dp = new int[text1.length() + 1][text2.length()+1];
        for (int i = 1; i < text1.length()+1; i++) {
            char c = text1.charAt(i-1);
            for (int j = 1; j < text2.length()+1; j++) {
                // 如果当前两个字符串相等，则取对角的值+1
                // 如果两个字符串不相等，则取同行的前一个和同列的上一个的最大值
                char cha = text2.charAt(j-1);
                if (c == cha){
                    dp[i][j] = dp[i-1][j-1] +1;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j] , dp[i][j-1]);
                }
            }
        }
        System.out.println(Arrays.deepToString(dp));
        return dp[text1.length()][text2.length()];
    }



}
